package com.zprmb.udzt.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;

import com.zprmb.udzt.BbtApplication;
import com.zprmb.udzt.R;
import com.zprmb.udzt.V4WebViewActivity2;
import com.zprmb.udzt.bean.HomeBean;
import com.zprmb.udzt.util.BitmapUtils;
import com.zprmb.udzt.util.Constant;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

public class ImageAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<HomeBean> ntw;
    private int windowWideh;
    private SharedPreferences sha;
    ViewHolder holder;

    public ImageAdapter(Context context, ArrayList<HomeBean> itemps) {
        mContext = context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ntw = itemps;
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager manager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        manager.getDefaultDisplay().getMetrics(dm);
        windowWideh = dm.widthPixels;
        sha = mContext.getSharedPreferences("config", mContext.MODE_PRIVATE);
    }

    public int getCount() {
        return Short.MAX_VALUE;   //返回很大的值使得getView中的position不断增大来实现循环
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.image_viewflow_item, null);
            holder.iv_viewflow_item_image = (ImageView) convertView.findViewById(R.id.iv_viewflow_item_image);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.iv_viewflow_item_image.setLayoutParams(new LayoutParams(windowWideh, LayoutParams.MATCH_PARENT));
        BbtApplication.getInstance().display(ntw.get(position % ntw.size()).getContent(), holder.iv_viewflow_item_image);
//		getBitmap(ntw.get(position%ntw.size()).getContent());
        convertView.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Editor editor = sha.edit();
                editor.putString("banner", ntw.get(position % ntw.size()).getUrl());
                editor.commit();
//				Log.e("banner_url", ntw.get(position%ntw.size()).getUrl()+"?app=true");／
                Intent intent = new Intent(mContext, V4WebViewActivity2.class);//WebViewThreeActivity
                if (ntw.get(position % ntw.size()).getUrl().contains("?")) {
                    if (ntw.get(position % ntw.size()).getUrl().contains("http")) {
                        intent.putExtra("url", ntw.get(position % ntw.size()).getUrl() + "&app=true&banner=true&appversion=true");
                    } else {
                        intent.putExtra("url", Constant.URL + ntw.get(position % ntw.size()).getUrl() + "&app=true&banner=true&appversion=true");
                    }

                } else {
                    if (ntw.get(position % ntw.size()).getUrl().contains("http")) {
                        intent.putExtra("url", ntw.get(position % ntw.size()).getUrl() + "?app=true&banner=true&appversion=true");
                    } else {
                        intent.putExtra("url", Constant.URL + ntw.get(position % ntw.size()).getUrl() + "?app=true&banner=true&appversion=true");
                    }
                }
                intent.putExtra("content", "优点智投");//title
                intent.putExtra("flag", 1);
                intent.putExtra("banner", 1);
                intent.putExtra("title", "优点智投");
                if (ntw.get(position % ntw.size()).getUrl().length() > 1) {
                    if (ntw.get(position % ntw.size()).getContent().equals("regist")) {
                       /* Intent intent1 = new Intent(mContext, RegisteredActivity.class);
                        mContext.startActivity(intent1);*/
                    } else {
                        mContext.startActivity(intent);
                    }
                }
            }
        });
        return convertView;
    }

    public void getBitmap(final String url) {

        new Thread() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                super.run();

                try {
                    Bitmap bm = null;

                    URL iconUrl = new URL(url);
                    URLConnection conn = iconUrl.openConnection();
                    HttpURLConnection http = (HttpURLConnection) conn;

                    int length = http.getContentLength();

                    conn.connect();
                    // 获得图像的字符流
                    InputStream is = conn.getInputStream();
                    BufferedInputStream bis = new BufferedInputStream(is, length);
                    bm = BitmapFactory.decodeStream(bis);
//    	            Message message=new Message();
//    	            message.obj=bm;
//    	            message.what=0;
//    	            handler.sendMessage(message);
                    holder.iv_viewflow_item_image.setImageBitmap(BitmapUtils.compressImage(bm));
                    bis.close();
                    is.close();// 关闭流

                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e("转换异常", e.toString());
                }
            }
        }.start();


    }

    public class ViewHolder {
        ImageView iv_viewflow_item_image;
    }
}
